function createList(pageNo = 1, pageSize = 10) {


    let unionID = $("#unionIDTxt").val();
    let matchData = {
        'isRobot': true,
        'unionID': {
            "$ne": 0
        }
    };
    if (unionID) {
        matchData['unionID'] = unionID;
    }
    let selectType = $("#selectType").val();
    if (selectType && selectType != '0') {
        matchData['gameType'] = selectType;
    }
    var start = (pageNo - 1) * pageSize;
    apiAdminGetRecord("userModel", start, pageSize, matchData, function (data) {
        if (data.code === 0) {
            setTimeout(() => {
                $('#robotData').bootstrapTable('load', data.msg);
            }, 200)
        }
    });
}


function addType() {

    var row = $("#robotData").bootstrapTable('getSelections');
    if (row.length > 0) {
        let list = [];
        for (let i = 0; i < row.length; i++) {
            list.push(row[i].uid);
        }
        $("#reAddUserID").val(list);
        $("#reAddUserID").attr("disabled", true);
        $('#addType').modal('show');
    } else {

        $.modal({
            body: '请先选择机器人'
        });
    }
}



function addRobotToGameType() {

    let uid = $("#reAddUserID").val();
    let gameType = $("#reGameType").val();
    let addWinRate = $("#addWinRate").val() || 50;
    let addWinType = $("#addWinType").val();
    let robotType = $("#robotType").val();
    $('#addType').modal('hide');
    let updateInfo = {
        gameType,
        winType: addWinType,
        winRate: addWinRate,
        robotType,
    }
    apiReGameType(uid, JSON.stringify(updateInfo), () => {
        createList(1);
    });
}




//启用 停用机器人
function robotBand(uid, isBind) {

    apiRobotBand(uid, isBind, () => {
        $('#robotData').bootstrapTable('refresh', {});
    });

}

function robotReback(uid) {

    apiRebackRobot(uid, () => {
        createList(1);
    });

}

function addOrderUinon(uid) {

    $("#addUserID").val(uid);
    $("#addUserID").attr("disabled", "true");
    $('#addOrder').modal('show');
}


//将机器人加到联盟中
function addRobotToUnion() {

    let uid = $("#addUserID").val();
    let unionID = $("#addUnionID").val();
    $("#addUserID").val("");
    $("#addUnionID").val("");
    $('#addOrder').modal('hide');
    apiRobotToUnion(uid, unionID, () => {
        $('#robotData').bootstrapTable('refresh', {});
    });

}



$(document).ready(function () {

    $(function () {

        //数据表格初始化
        $('#robotData').bootstrapTable({
            striped: true, // 是否显示行间隔色
            pageNumber: 1, // 初始化加载第一页
            pagination: true, // 是否分页
            clickToSelect: true,
            theadClasses: "thead-light", //这里设置表头样式
            sidePagination: 'server', // server:服务器端分页|client：前端分页
            pageSize: 10, // 单页记录数
            pageList: [10, 20, 30],
            dataField: "recordArr",
            totalField: "totalCount",
            buttonsClass: 'light',
            buttonsPrefix: 'btn',
            showRefresh: false, // 刷新按钮
            onPageChange: function (number, size) {
                createList(number, size);
            },
            queryParams: function (params) { // 上传服务器的参数
                var temp = { //如果是在服务器端实现分页，limit、offset这两个参数是必须的
                    limit: params.limit, // 每页显示数量
                    offset: params.offset, // SQL语句起始索引
                };
                return temp;
            },
            columns: [{
                checkbox: true,
                align: "center",
                valign: "middle",
            }, {
                title: 'id',
                field: '_id',
                visible: false
            },
            {
                title: 'UID',
                width: 100,
                align: "center",
                valign: "middle",
                field: 'uid',
            }, {
                title: '昵称',
                width: 100,
                align: "center",
                valign: "middle",
                field: 'nickname',

            }, {
                title: '头像',
                field: 'avatar',
                width: 100,
                align: "center",
                valign: "middle",
                formatter: avaterImg
            }, {
                title: '联盟ID',
                width: 100,
                align: "center",
                valign: "middle",
                field: 'unionID',
                sortable: true,
                order: "asc",

            }, {
                title: '类型',
                width: 100,
                align: "center",
                valign: "middle",
                field: 'robotType',
                formatter: robotTypeSet
            }, {
                title: '游戏类型',
                width: 100,
                align: "center",
                valign: "middle",
                field: 'gameType',
                formatter: gameTypeSet
            }, {
                title: '控制类型',
                field: 'winType',
                width: 100,
                align: "center",
                valign: "middle",
                formatter: winTypeSet
            }, {
                title: '输赢比例',
                field: 'winRate',
                width: 100,
                align: "center",
                valign: "middle"
            },
            {
                title: 'IP地址',
                width: 200,
                align: "center",
                valign: "middle",
                field: 'lastLoginIP'
            }, {
                field: 'createTime',
                align: "center",
                valign: "middle",
                width: 150,
                title: '创建时间',
                formatter: function (value) {
                    return new Date(value).toLocaleString();
                }
            },
            {
                title: '操作',
                width: 200,
                align: "center",
                valign: "middle",
                formatter: option
            }
            ]
        });
        createList(1);
    })

    //机器人控制类型0：正常 1：输 2：赢
    function winTypeSet(value, row, index) {
        if (value == 2) {
            return '赢';
        } else if (value == 1) {
            return '输';
        } else {
            return '正常';
        }
    }
    // 定义删除、更新按钮
    function robotTypeSet(value, row, index) {

        if (value == 0) {
            return '气氛组';
        } else if (value == 1) {
            return '被动陪打';
        } else if (value == 2) {
            return '主动陪打';
        } else {
            return '暂未设置';
        }
    }

    function gameTypeSet(value, row, index) {

        if (value == 0) {
            return '全部';
        } else if (value == 1) {
            return '炸金花';
        } else if (value == 2) {
            return '牛牛';
        } else if (value == 3) {
            return '跑的快';
        } else if (value == 4) {
            return '三公';
        } else if (value == 5) {
            return '红中麻将';
        } else if (value == 8) {
            return '斗公牛';
        } else if (value == 9) {
            return '十三水';
        } else if (value == 10) {
            return '血战麻将';
        } else if (value == 11) {
            return '关春天';
        } else if (value == 12) {
            return '捞腌菜';
        } else if (value == 13) {
            return '飞小鸡';
        } else if (value == 14) {
            return '三公比金花';
        } else if (value == 15) {
            return '十点半';
        } else if (value == 22) {
            return '冕宁跑得快';
        } else if (value == 28) {
            return '加锅推筒子';
        } else if (value == 30) {
            return '福建十三水';
        } else if (value == 31) {
            return '泉州麻将';
        } else {
            return "未设置"
        }
    }



    // 定义删除、更新按钮
    function avaterImg(value, row, index) {
        var htm = "";
        htm = `<img  style="width: 40px ; height: 40px;"  src="${value}" class="img-thumbnail" >`;
        return htm;
    }

    // 定义删除、更新按钮
    function option(value, row, index) {
        var htm = "";
        htm += `<button type="button" class="btn btn-danger btn-sm" onClick="robotReback(${row.uid})">回收</button>`;
        return htm;
    }

});